package com.szh.library.book.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szh.common.enums.ResultCodeEnum;
import com.szh.common.result.Result;
import com.szh.library.book.service.BookService;
import com.szh.model.Book;
import com.szh.model.User;
import com.szh.vo.BookFindVo;
import com.szh.vo.BookSaveVo;
import com.szh.vo.BookUpdateVo;
import com.szh.vo.UserSelectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: SongZiHao
 * @date: 2023/5/10
 */
@Api(tags = "图书管理")
@RestController
@RequestMapping(value = "/library/book")
public class BookController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BookController.class);

    @Autowired
    private BookService bookService;

    @ApiOperation(value = "根据id查询图书")
    @GetMapping(value = "/findBookById/{id}")
    public Result<Book> findBookById(@ApiParam(name = "id", value = "图书id", required = true)
                                     @PathVariable("id") Long id) {
        Book book = bookService.findBookById(id);
        return Result.build(book, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "查询所有图书")
    @GetMapping(value = "/findAllBooks")
    public Result<List<Book>> findAllBooks() {
        List<Book> books = bookService.findAllBooks();
        return Result.build(books, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "条件查询图书，带分页")
    @PostMapping(value = "/findBooksByConditionPage/{page}/{limit}")
    public Result<Page<Book>> findBooksByConditionPage(@ApiParam(name = "page", value = "页码", required = true)
                                                        @PathVariable("page") Long page,
                                                        @ApiParam(name = "limit", value = "每页记录数", required = true)
                                                        @PathVariable("limit") Long limit,
                                                        @ApiParam(name = "bookFindVo", value = "查询条件vo类")
                                                        @RequestBody(required = false) BookFindVo bookFindVo) {
        Page<Book> pageModel = bookService.findBooksByConditionPage(page, limit, bookFindVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "保存图书")
    @PostMapping(value = "/saveBook")
    public Result saveBook(@ApiParam(name = "bookSaveVo", value = "图书保存vo类", required = true)
                           @RequestBody BookSaveVo bookSaveVo) {
        return bookService.saveBook(bookSaveVo) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "根据id修改图书")
    @PutMapping(value = "/updateBook")
    public Result updateBook(@ApiParam(name = "bookUpdateVo", value = "图书修改vo类", required = true)
                             @RequestBody BookUpdateVo bookUpdateVo) {
        return bookService.updateBook(bookUpdateVo) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "根据id删除图书")
    @DeleteMapping(value = "/deleteBookById/{id}")
    public Result deleteBookById(@ApiParam(name = "id", value = "待删图书id", required = true)
                                 @PathVariable("id") Long id) {
        return bookService.deleteBookById(id) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "批量删除图书")
    @DeleteMapping(value = "/deleteBatchByIds")
    public Result deleteBatchByIds(@ApiParam(name = "bookIds", value = "待删图书id集合", required = true)
                                   @RequestBody List<Long> bookIds) {
        return bookService.deleteBatchByIds(bookIds) ? Result.ok() : Result.fail();
    }

    //--------------------------- 以下接口只为回顾OpenFeign远程接口调用 ---------------------------

    @ApiOperation(value = "远程接口调用：根据id查询用户")
    @GetMapping(value = "/openFeign/user/selectUserById/{id}")
    public Result<User> openFeignSelectUserById(@ApiParam(name = "id", value = "用户id", required = true)
                                                @PathVariable("id") Long id) {
        User user = bookService.openFeignSelectUserById(id);
        return Result.build(user, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "远程接口调用：查询所有用户")
    @GetMapping(value = "/openFeign/user/selectAllUsers")
    public Result<List<User>> openFeignSelectAllUsers() {
        List<User> userList = bookService.openFeignSelectAllUsers();
        return Result.build(userList, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "远程接口调用：条件查询用户，带分页")
    @PostMapping(value = "/openFeign/user/selectUsersByConditionPage/{page}/{limit}")
    public Result<Page<User>> openFeignSelectUsersByConditionPage(@ApiParam(name = "page", value = "页码", required = true)
                                                                  @PathVariable("page") Long page,
                                                                  @ApiParam(name = "limit", value = "每页记录数", required = true)
                                                                  @PathVariable("limit") Long limit,
                                                                  @ApiParam(name = "userSelectVo", value = "查询条件vo类")
                                                                  @RequestBody(required = false) UserSelectVo userSelectVo) {
        Page<User> pageModel = bookService.openFeignSelectUsersByConditionPage(page, limit, userSelectVo);
        return Result.ok(pageModel);
    }
}
